package binarysearchtree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-04-19
 * Time：23:40
 */



public class TestBinarySearchTree {
    public static void main1(String[] args) {
        MyBinarySearchTree mvst=new MyBinarySearchTree();

        // 最先插入的为 头节点
        mvst.insert(50);
        mvst.insert(40);
        mvst.insert(70);
        mvst.insert(20);

        mvst.insert(45);
        mvst.insert(60);
        mvst.insert(80);
        System.out.println(mvst.search(50).val);
        System.out.println();
        mvst.inorder(MyBinarySearchTree.root);
        System.out.println();
        mvst.remove(50);
        mvst.inorder(MyBinarySearchTree.root);

        System.out.println();


    }


    public static void main(String[] args) {
        MyBinarySearchTree mv=new MyBinarySearchTree();
        mv.insert(10);
        mv.insert(6);
        mv.insert(14);
        mv.insert(4);
        mv.insert(8);
        mv.insert(12);
        mv.insert(16);
        Convert(MyBinarySearchTree.root);
    }

    public static MyBinarySearchTree.TreeNode Convert(MyBinarySearchTree.TreeNode pRootOfTree) {
        Queue<MyBinarySearchTree.TreeNode> queue=new LinkedList<>();
        createTree(pRootOfTree,queue);

        MyBinarySearchTree.TreeNode head = queue.poll();
        head.left=null;
        MyBinarySearchTree.TreeNode cur=head;
        while(!queue.isEmpty()) {

            MyBinarySearchTree.TreeNode node= queue.poll();
            cur.right=node;
            node.left=cur;
            cur=cur.right;
        }
        cur.right=null;
        return head;
    }

    private  static void createTree(MyBinarySearchTree.TreeNode root, Queue<MyBinarySearchTree.TreeNode> queue) {
        if(root==null) {
            return;
        }
        createTree(root.left,queue);
        queue.add(root);
        createTree(root.right,queue);
    }
}
